namespace ObjectPersistence {
    using System;
    using System.Text;
    using System.Collections;
    using System.Data.Common;
    using System.Transactions;
    using System.Collections.Generic;
    
    public partial class ModuleAssembler : AssemblerBaseUpdatable<KYO.DAL.Module, ModuleCriteria, ModuleRowMap, ModuleDomainObjectFactory> {
        public ModuleAssembler() {
            base.connection = new System.Data.SqlClient.SqlConnection(GlobalUtil.Constants.ConnectionString);
        }
        protected override System.Data.Common.DbConnection InstanceConnection {
            get {
                return this.connection;
            }
        }
        /// <summary>Reads a single object from the database corresponding to the object keys which are passed in</summary>
        public KYO.DAL.Module ReadSingleByKey(int ModuleId) {
            ModuleCriteria criteria = new ModuleCriteria();
            criteria.Set_ModuleId(ModuleCriteria.AVAILABLE_OPERATORS.EQUALS, ModuleId);
            return this.ReadSingle(criteria);
        }

        internal override void MakePersistent(KYO.DAL.Module PersistentObject, bool cascade, System.Data.Common.DbCommand comm) {
            if ((PersistentObject.IsNew == false)) {
                if ((GlobalUtil.Constants.ObjectMapKYOPHUONGDOPC.Contains(PersistentObject.ROKeyString) 
                            && (PersistentObject.ChangesMade == false))) {
                    return;
                }
                if ((GlobalUtil.Constants.ObjectMapKYOPHUONGDOPC.Contains(PersistentObject.ROKeyString) == false)) {
                    GlobalUtil.Constants.ObjectMapKYOPHUONGDOPC.Add(PersistentObject.ROKeyString);
                }
            }

            if ((PersistentObject.IsNew || PersistentObject.ChangesMade)) {
                ModuleUpdateFactory updateFactory = new ModuleUpdateFactory();
                comm.Parameters.Clear();
                if ((PersistentObject.IsNew == false)) {
                    updateFactory.updateCommandText(PersistentObject, comm);
                    int numRows = comm.ExecuteNonQuery();
                    if ((numRows == 0)) {
                        throw new NoRowsUpdated();
                    }
                }
                else {
                    updateFactory.insertCommandText(PersistentObject, comm);
                    comm.CommandText = (comm.CommandText + "; SELECT SCOPE_IDENTITY();");
                    decimal NewIdentityKey = ((decimal)(comm.ExecuteScalar()));
                    PersistentObject.ModuleId = ((int)(NewIdentityKey));
                    GlobalUtil.Constants.ObjectMapKYOPHUONGDOPC.Add(PersistentObject.ROKeyString);
                    PersistentObject.IsNew = false;
                }
            }
            PersistentObject.ChangesMade = false;
            if (cascade) {
                ModuleIntallAssembler tempModuleIntallAssembler = new ModuleIntallAssembler();

                if ((PersistentObject.IsModuleIntallCollectionNull() == false)) {
                    for (int i1 = 0; (i1 < PersistentObject.ModuleIntallCollection.Count); i1 = (i1 + 1)) {
                        if (((PersistentObject.ModuleIntallCollection[i1].ModuleId == PersistentObject.ModuleId) 
                                    == false)) {
                            PersistentObject.ModuleIntallCollection[i1].ModuleId = PersistentObject.ModuleId;
                        }
                        comm.Parameters.Clear();
                        tempModuleIntallAssembler.MakePersistent(PersistentObject.ModuleIntallCollection[i1], cascade, comm);
                    }
                    PersistentObject.ModuleIntallCollection.Clean();
                    for (int i1a = 0; (i1a < PersistentObject.ModuleIntallCollection.Deleted.Count); i1a = (i1a + 1)) {
                        tempModuleIntallAssembler.Delete(PersistentObject.ModuleIntallCollection.Deleted[i1a]);
                    }
                    PersistentObject.ModuleIntallCollection.Synch();
                }
                tempModuleIntallAssembler.Dispose();
            }
        }

        internal override void Delete(KYO.DAL.Module PersistentObject, System.Data.Common.DbCommand comm) {
            comm.Parameters.Clear();

            ModuleSelectionFactory tempModuleSelectionFactory = new ModuleSelectionFactory();
            string sql = "DELETE FROM [dbo].[Module]";
            comm.CommandText = sql;
            tempModuleSelectionFactory.getDefaultSelection(PersistentObject, comm);
            comm.ExecuteNonQuery();
        }

        #region Manage Child Objects / Collections
        /// Reads the KYO.DAL.ModuleIntall from the database corresponding to the object keys passed as parameters
        // <param name="localModuleId">Corresponds to Module attribute ModuleId</param
        public BusinessLayerHelper.PersistentList<KYO.DAL.ModuleIntall> GetModuleIntallCollection(int localModuleId) {
            BusinessLayerHelper.PersistentList<KYO.DAL.ModuleIntall> result = new BusinessLayerHelper.PersistentList<KYO.DAL.ModuleIntall>();
            ModuleIntallCriteria ModuleIntallCriteriaSingle = new ModuleIntallCriteria();
            ModuleIntallCriteriaSingle.Set_ModuleId(ModuleIntallCriteria.AVAILABLE_OPERATORS.EQUALS, localModuleId);
            ModuleIntallAssembler _tempAssem = new ModuleIntallAssembler();
            result.FillData(_tempAssem.ReadCollection(ModuleIntallCriteriaSingle));
            return result;
        }
        #endregion

        #region Helper Utilities
        protected override ModuleRowMap fillRowMap(System.Data.Common.DbDataReader reader) {
            ModuleRowMap result = new ModuleRowMap();
            result.ModuleId = reader.GetInt32(reader.GetOrdinal("dboModuleDOTModuleId"));
            result.Name = reader.GetString(reader.GetOrdinal("dboModuleDOTName"));
            result.Author = reader.GetString(reader.GetOrdinal("dboModuleDOTAuthor"));
            result.CopyRight = reader.GetString(reader.GetOrdinal("dboModuleDOTCopyRight"));
            result.Version = reader.GetString(reader.GetOrdinal("dboModuleDOTVersion"));
            result.Description = reader.GetString(reader.GetOrdinal("dboModuleDOTDescription"));
            if ((reader.IsDBNull(reader.GetOrdinal("dboModuleDOTModulePath")) == false)) {
                result.ModulePath = reader.GetString(reader.GetOrdinal("dboModuleDOTModulePath"));
            }
            if ((reader.IsDBNull(reader.GetOrdinal("dboModuleDOTFileName")) == false)) {
                result.FileName = reader.GetString(reader.GetOrdinal("dboModuleDOTFileName"));
            }
            return result;
        }
        internal override void ClearList() {
            GlobalUtil.Constants.ObjectMapKYOPHUONGDOPC.Clear();
        }

        public override string Abstract_SELECT {
            get {
                return ("SELECT " + ModuleAssembler.RawFields);
            }
        }
        protected override string InstanceTable {
            get {
                return "[dbo].[Module]";
            }
        }
        public const string RawFields = @"[dbo].[Module].[ModuleId] AS dboModuleDOTModuleId, [dbo].[Module].[Author] AS dboModuleDOTAuthor, [dbo].[Module].[CopyRight] AS dboModuleDOTCopyRight, [dbo].[Module].[Description] AS dboModuleDOTDescription, [dbo].[Module].[FileName] AS dboModuleDOTFileName, [dbo].[Module].[ModulePath] AS dboModuleDOTModulePath, [dbo].[Module].[Name] AS dboModuleDOTName, [dbo].[Module].[Version] AS dboModuleDOTVersion";
        public const string SELECT = @"SELECT [dbo].[Module].[ModuleId] AS dboModuleDOTModuleId, [dbo].[Module].[Author] AS dboModuleDOTAuthor, [dbo].[Module].[CopyRight] AS dboModuleDOTCopyRight, [dbo].[Module].[Description] AS dboModuleDOTDescription, [dbo].[Module].[FileName] AS dboModuleDOTFileName, [dbo].[Module].[ModulePath] AS dboModuleDOTModulePath, [dbo].[Module].[Name] AS dboModuleDOTName, [dbo].[Module].[Version] AS dboModuleDOTVersion";
        #endregion
    }
}
